package com.changwu.cwbase.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @ PKG NAME   :com.hnlens.app.devicemanagement.util
 * @ CLS NAME   :DateUtil
 * @ AUT NAME   :LL124478
 * @ DAT TIME   :12/5/2017 1:34 PM
 * @ SPE NAME   :日期时间工具类
 */
public class DateUtil {
    public static final int HOUR = 0;
    public static final int MINUTE = 1;

    public static final String DATE1 = "yyyy-MM-dd";
    public static final String DATE2 = "yyyy/MM/dd";
    public static final String TIME1 = "HH:mm:ss";
    public static final String TIME2 = "hh:mm:ss";
    public static final String TIME3 = "hh:mm";
    public static final String TIME4 = "HH:mm";
    public static final String DATE_TIME1 = DATE1 + " " + TIME1;
    public static final String DATE_TIME2 = DATE1 + " " + TIME2;
    public static final String DATE_TIME3 = DATE2 + " " + TIME1;
    public static final String DATE_TIME4 = DATE2 + " " + TIME2;
    public static final String DATE_TIME5 = DATE1 + " " + TIME4;
    public static final String DATE_TIME6 = DATE2 + " " + TIME4;

    private SimpleDateFormat mSimpleDateFormat;

    public static String formatDate(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String formatDate(long date, String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(date));
    }


    public static String formatDate(String date) {
        //"2016-06-21T00:00:00
        if (TextUtils.isEmpty(date)) {
            return "";
        }
        int index = date.indexOf("T");
        if (index != -1) {
            return date.substring(0, index);
        } else {
            return date;
        }


    }

    public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));

        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
            } else if (strDateH == strDateEndH && strDateM <= strDateEndM) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    public static Date stringToDate(String datetime) {
        return stringToDate(datetime, "yyyy-MM-dd HH:mm");
    }

    public static Calendar stringToCalendar(String datetime) {
        return stringToCalendar(datetime, "yyyy-MM-dd HH:mm");
    }

    public static Calendar string1ToCalendar(String datetime) {
        return stringToCalendar(datetime, "yyyy-MM-dd");
    }

    public static Calendar stringToCalendar(String datetime, String patten) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patten, Locale.CHINA);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        try {
            calendar.setTime(simpleDateFormat.parse(datetime));
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
            return calendar;
        }
    }

    public static Date stringToDate(String datetime, String patten) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patten, Locale.CHINA);
        try {
            return simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static Date stringToDate3(String datetime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        try {
            return simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static Date stringToDate2(String datetime) {
        if (!TextUtils.isEmpty(datetime)) {
            int index = datetime.indexOf("T");
            if (index != -1) {
                datetime = datetime.replace("T", " ");
            }
            return stringToDate(datetime);
        } else {
            return new Date();
        }

    }


    public static int calculateDateTime(Date startDate, Date endData) {
        int result = 0;

        try {
            long diff = endData.getTime() - startDate.getTime();
            result = (int) (diff / (1000 * 60));

        } catch (Exception e) {
            result = 0;
        }
        return result;
    }
    public static long[] getDiffDateTime(long startDate, long endData) {
        long mDay, mHour, mMin, mSecond;
        long[] result = new long[4];
        try {
            long diff = endData - startDate;
            mDay = diff / (24 * 60 * 60 * 1000);
            mHour = (diff / (60 * 60 * 1000) - mDay * 24);
            mMin = ((diff / (60 * 1000)) - mDay * 24 * 60 - mHour * 60);
            mSecond = (diff / 1000 - mDay * 24 * 60 * 60 - mHour * 60 * 60 - mMin * 60);
            result[0]=mDay;
            result[1]=mHour;
            result[2]=mMin;
            result[3]=mSecond;
        } catch (Exception e) {
        }
        return result;
    }

    public static String timeToString(int minute) {
        if (minute < 60) {
            return "0时" + minute + "分";
        } else {
            int hour = minute / 60;
            int mi = minute % 60;
            return hour + "时" + mi + "分";
        }
    }

    public static String timeToString2(int minute) {
        if (minute < 0) return "0天 " + "00:00";
        if (minute < 60) {
            return "0天 " + "00:" + (minute > 9 ? "" + minute : "0" + minute);
        } else if (minute < 60 * 24) {
            int hour = minute / 60;
            int mi = minute % 60;
            return "0天 " + (hour > 9 ? "" + hour : "0" + hour) + ":" + (mi > 9 ? "" + mi : "0" + mi);
        } else {
            int day = minute / (60 * 24);
            int hour = minute % (60 * 24) / 60;
            int mi = minute % (60 * 24) % 60;
            return day + "天 " + (hour > 9 ? "" + hour : "0" + hour) + ":" + (mi > 9 ? "" + mi : "0" + mi);
        }
    }

    public static String getMonthAgoCalendar(Calendar calendar, String pattern) {
        calendar.add(Calendar.MONTH, -1);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static Calendar getDayAgoCalendar(Calendar calendar, String pattern) {
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar;
    }

    public static Calendar getDayAfterCalendar(Calendar calendar, String pattern) {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar;
    }
}
